import { createMemoryHistory, createRouter } from "vue-router";

// 初始界面
import LoginView from "@/views/init/login/index.vue";
import RegisterView from "@/views/init/register/index.vue";

import StudentLayout from "@/views/student/layout/index.vue";
// 主界面
// import MainView from "@/views/main/index.vue";
// import HomeView from "@/views/main/home/index.vue";
// import CourseView from "@/views/main/course/index.vue";
// import EvaluationView from "@/views/main/evaluation/index.vue";
// import NoteView from "@/views/main/notes/index.vue";
// import RaceView from "@/views/main/race/index.vue";
// import ProductView from "@/views/main/product/index.vue";
// import ShareView from "@/views/main/share/index.vue";
// import DiscussionView from "@/views/main/discussion/index.vue";
// import { ElMessage } from "element-plus";

// import GameView from "@/views/main/game/index.vue";

import { createWebHashHistory } from "vue-router";

const routes = [
  { path: "/", redirect: "/login" },
  {
    path: "/login",
    component: LoginView,
  },

  {
    path: "/register",
    component: RegisterView,
  },
  {
    path: "/student",
    component: StudentLayout,
    redirect: "/student/home",
    meta: { requiresAuth: true, role: "student" },
    children: [
      {
        path: "home",
        name: "StudentHome",
        component: () => import("@/views/student/home/index.vue"),
      },
      {
        path: "course",
        name: "StudentCourses",
        component: () => import("@/views/student/course/index.vue"),
      },
      {
        path: "discussion",
        name: "StudentDiscussions",
        component: () => import("@/views/student/discussion/index.vue"),
      },
      {
        path: "share",
        name: "StudentShare",
        component: () => import("@/views/student/share/index.vue"),
      },
    ],
  },
  {
    path: "/teacher",
    redirect: "/teacher/home",
    component: () => import("@/views/teacher/layout/index.vue"),
    redirect: "/teacher/home",
    meta: { requiresAuth: true, role: "teacher" },
    children: [
      {
        path: "home",
        name: "TeacherHome",
        component: () => import("@/views/teacher/home/index.vue"),
      },
      {
        path: "user",
        name: "TeacherUser",
        component: () => import("@/views/teacher/user/index.vue"),
      },
      {
        path: "course",
        name: "TeacherCourse",
        component: () => import("@/views/teacher/course/index.vue"),
      },
      {
        path: "share",
        name: "TeacherShare",
        component: () => import("@/views/teacher/share/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  let isAuthenticated = !!sessionStorage.getItem("userInfo");
  console.log(isAuthenticated);
  if (
    to.path !== "/login" &&
    to.path !== "/register" &&
    to.path !== "/reset" &&
    !isAuthenticated
  ) {
    next({ path: "/login" });
    ElMessage({
      message: "请先登录！",
      type: "warning",
    });
  } else {
    next();
  }
});

export default router;
